
main_df$group.dictator_payoff_thou <- main_df$group.dictator_payoff/1000

plot_dat <-
  main_df %>%
  filter(pub_official_indicator == 1) %>%
  group_by(group.treatment) %>%
  summarise(keep = mean(group.dictator_payoff_thou, na.rm = T),
            herf = mean(herf_index_payout, na.rm = T),
            others = mean(perc_other, na.rm = T)) %>%
  pivot_longer(cols = keep:others, names_to = "cols", values_to = "values")

se1 <- Rmisc::summarySE(main_df %>%filter(pub_official_indicator == 1), measurevar= "group.dictator_payoff_thou", groupvars=c("group.treatment"))%>% select(group.treatment, se) %>% mutate(cols = "keep")
se2 <- Rmisc::summarySE(main_df %>%filter(pub_official_indicator == 1), measurevar= "herf_index_payout", groupvars=c("group.treatment")) %>% select(group.treatment, se) %>% mutate(cols = "herf")
se3 <- Rmisc::summarySE(main_df %>%filter(pub_official_indicator == 1), measurevar= "perc_other", groupvars=c("group.treatment")) %>% select(group.treatment, se) %>% mutate(cols = "others")

plot_dat <-
  plot_dat %>%
  left_join(., bind_rows(se1, se2, se3))

kept_fig <- 
  plot_dat %>%
  filter(cols == "keep") %>%
  ggplot(aes(x=group.treatment, y = values, fill = group.treatment)) +
  geom_bar(stat = "identity", color = "black") +
  geom_errorbar(aes(x=group.treatment, ymin = values - 1.67*se, ymax = values + 1.67*se), width = 0.05) +
  geom_errorbar(aes(x=group.treatment, ymin = values - 1.96*se, ymax = values + 1.96*se), width = 0.075) +
  xlim(c("patronage", "random", "merit")) +
  #facet_wrap(cols~., scales = "free_y") +
  ylab("Money Kept by Public Official (000s IDR)") +
  theme_bw()+ 
  theme(axis.title.x = element_blank(),
        axis.line.x.bottom = element_line(color = "black"),
        axis.line.y.left = element_line(color = "black"),
        text = element_text(size=14),
        panel.grid.major = element_blank(),
        legend.position = "none",
        panel.border = element_blank(),
        axis.ticks = element_blank()) +
  scale_fill_grey() +
  geom_segment(aes(x = 1.1, xend = 1.9, yend = 145, y = 145))+
  geom_segment(aes(x = 1.9, xend = 2.0, yend = 140, y = 145))+
  geom_segment(aes(x = 1.1, xend = 1.0, yend = 140, y = 145))+
  geom_segment(aes(x = 2.1, xend = 2.9, yend = 145, y = 145))+
  geom_segment(aes(x = 2.9, xend = 3.0, yend = 140, y = 145)) +
  geom_segment(aes(x = 2.1, xend = 2, yend = 140, y = 145)) +
  annotate("text", x=1.5, y=155, label="-11.0k (p=0.171)", size=4.5) +
  annotate("text", x=2.5, y=155, label="+20.2k (p=0.011)", size=4.5) +
  geom_segment(aes(x = 1.1, xend = 2.9, yend = 165, y = 165))+
  geom_segment(aes(x = 2.9, xend = 3.0, yend = 160, y = 165)) +
  geom_segment(aes(x = 1.1, xend = 1, yend = 160, y = 165)) +
  annotate("text", x=2, y=170, label="+9.2k (p=0.281)", size=4.5)



herf_fig <-
  plot_dat %>%
  filter(cols == "herf") %>%
  ggplot(aes(x=group.treatment, y = values, fill = group.treatment)) +
  geom_bar(stat = "identity", color = "black") +
  geom_errorbar(aes(x=group.treatment, ymin = values - 1.67*se, ymax = values + 1.67*se), width = 0.075) +
  geom_errorbar(aes(x=group.treatment, ymin = values - 1.96*se, ymax = values + 1.96*se), width = 0.05) +
  
  #facet_wrap(cols~., scales = "free_y") +
  ylab("Herfindahl Index (Money Fractionalization, 0-1)") +
  theme_bw()+ 
  theme(axis.title.x = element_blank(),
        axis.line.x.bottom = element_line(color = "black"),
        axis.line.y.left = element_line(color = "black"),
        text = element_text(size=14),
        panel.grid.major = element_blank(),
        legend.position = "none",
        panel.border = element_blank(),
        axis.ticks = element_blank()) +
  scale_fill_grey() +
  xlim(c("patronage", "random", "merit")) +
  geom_segment(aes(x = 1.1, xend = 1.9, yend = 0.7, y = 0.7))+
  geom_segment(aes(x = 1.9, xend = 2.0, yend = 0.67, y = 0.7))+
  geom_segment(aes(x = 1.1, xend = 1.0, yend = 0.67, y = 0.7))+
  geom_segment(aes(x = 2.1, xend = 2.9, yend = 0.7, y = 0.7))+
  geom_segment(aes(x = 2.9, xend = 3.0, yend = 0.67, y = 0.7)) +
  geom_segment(aes(x = 2.1, xend = 2, yend = 0.67, y = 0.7)) +
  annotate("text", x=1.5, y=0.73, label="-0.051 (p=0.139)", size=4.5) +
  annotate("text", x=2.5, y=0.73, label="+0.096 (p=0.01)", size=4.5) +
  geom_segment(aes(x = 1.1, xend = 2.9, yend = 0.8, y = 0.8))+
  geom_segment(aes(x = 2.9, xend = 3.0, yend = 0.77, y = 0.8)) +
  geom_segment(aes(x = 1.1, xend = 1, yend = 0.77, y = 0.8)) +
  annotate("text", x=2, y=0.82, label="+0.044 (p=0.261)", size=4.5)


ggsave(filename = "./outputs/figures/figure_4a.pdf", plot = kept_fig, width = 7, height = 5)
ggsave(filename = "./outputs/figures/figure_4b.pdf", plot = herf_fig, width = 7, height = 5)
